package com.guigui.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;
import com.guigui.gulimall.ware.dao.PurchaseDetailDao;
import com.guigui.gulimall.ware.entity.PurchaseDetailEntity;
import com.guigui.gulimall.ware.service.PurchaseDetailService;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        /**
         * key:
         * status:
         * ware_id:
         */
        String status = (String) params.get("status");
        if (!StringUtils.isNullOrEmpty(status)) queryWrapper.eq("status", Integer.valueOf(status));

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isNullOrEmpty(wareId)) queryWrapper.eq("ware_id", Integer.valueOf(wareId));

        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)) {
            queryWrapper.and(q -> {
                q.eq("id", Long.valueOf(key))
                        .or().eq("purchase_id", Long.valueOf(key))
                        .or().eq("sku_id",Long.valueOf(key));
            });
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}